package com.reading.cms.controller.system;

import com.reading.cms.controller.BaseController;
import com.reading.cms.framework.web.page.TableDataInfo;
import com.reading.common.entity.system.Role;
import com.reading.common.entity.system.SysUser;
import com.reading.common.utils.JsonResult;
import com.reading.common.utils.StringUtil;
import com.reading.core.manager.system.IRoleManager;
import com.reading.core.manager.system.ISystemUserManager;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    private String prefix = "system/user";

    @Resource
    private ISystemUserManager userManager;

    @Resource
    private IRoleManager roleManager;

    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user() {
        return prefix + "/user";
    }

    @RequiresPermissions("system:sysUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser sysUser) {
        startPage();
        List<SysUser> list = userManager.selectUserList(sysUser);
        return getDataTable(list);
    }

    @PostMapping("/export")
    @ResponseBody
    public JsonResult export(SysUser sysUser) throws Exception {
        return error("导出Excel失败，请联系网站管理员！");
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("system:user:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Integer userId, Model model) {
        SysUser sysUser = userManager.selectUserById(userId);
        List<Role> roles = roleManager.selectRolesByUserId(userId);
        model.addAttribute("roles", roles);
        model.addAttribute("user", sysUser);
        return prefix + "/edit";
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("system:user:add")
    @GetMapping("/add")
    public String add(Model model) {
        List<Role> roles = roleManager.selectRoleAll();
        model.addAttribute("roles", roles);
        return prefix + "/add";
    }

    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Integer userId, Model model) {
        SysUser sysUser = userManager.selectUserById(userId);
        model.addAttribute("user", sysUser);
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:sysUser:resetPwd")
    @PostMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(SysUser sysUser) {
        int rows = userManager.resetUserPwd(sysUser);
        if (rows > 0) {
            return success();
        }
        return error();
    }

    @RequiresPermissions("system:user:remove")
    @PostMapping("/remove")
    @ResponseBody
    public JsonResult remove(String ids) {
        try {
            userManager.deleteUserByIds(ids);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 保存用户
     */
    @RequiresPermissions("system:sysUser:save")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public JsonResult save(SysUser sysUser) {
        if (StringUtil.isNotNull(sysUser.getUserId()) && SysUser.isAdmin(sysUser.getUserId())) {
            return error("不允许修改超级管理员用户");
        }
        if (StringUtil.isEmpty(sysUser.getInviteCode())){
            sysUser.setInviteCode("100");
            sysUser.setParentCode(getUser().getParentCode()+getUser().getInviteCode()+",");
        }
        return userManager.saveUser(sysUser) > 0 ? success() : error();
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUser sysUser) {
        String uniqueFlag = "0";
        if (sysUser != null) {
            uniqueFlag = userManager.checkLoginNameUnique(sysUser.getLoginName());
        }
        return uniqueFlag;
    }
}